<template>
  <div>
    <el-card shadow="hover" style="margin: 0 auto;width: 888px">
      <el-form ref="formData" :model="formData" :rules="rules" size="mini" label-width="100px"
               label-position="left">
        <el-form-item label="商家类型" prop="type">
          <el-select v-model="formData.type" placeholder="请选择商家类型" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in typeEnums" :key="index" :label="item" :value="item"
                       :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入商家名称" clearable prefix-icon='el-icon-goods'
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="法人" prop="corporation">
          <el-input v-model="formData.corporation" placeholder="请输入法人" clearable prefix-icon='el-icon-user'
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="省市县" prop="field104">
          <el-cascader
            v-model="field104"
            :options="ssx"
            clearable
            :props="addprop"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入详细地址" clearable prefix-icon='el-icon-s-promotion'
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="管理员">
          <el-input v-model="this.userInfo.userName" placeholder="请输入管理员" clearable prefix-icon='el-icon-s-flag'
                    :disabled="true"
                    :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="统一征信码" prop="certNum">
          <el-input v-model="formData.certNum" placeholder="请输入统一征信码" clearable prefix-icon='el-icon-s-release'
                    :style="{width: '100%'}"></el-input>
        </el-form-item>

        <el-form-item label="营业执照" prop="headPicture" required>

          <el-upload ref="headPicture"
                     action="#"
                     :http-request="touploadHead"
                     class="avatar-uploader"
                     :on-success="handlerSuccess"
                     :show-file-list="false"
                     :before-upload="headPictureBeforeUpload" accept="image/*">
            <img v-if="this.formData.headPicture" :src="this.formData.headPicture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <!--            <i class="el-icon-plus"></i>-->
            <div slot="tip" class="el-upload__tip">只能上传不超过 10MB 的image/*文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item size="large">

          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import {getRegionList} from "@/api/housing/roomRack/roomRack";
import {businessType, saveBusiness, auploadHead} from "@/api/system/business";
import {getInfo} from "@/api/login";

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        type: undefined,
        name: undefined,
        corporation: undefined,
        address: undefined,
        certNum: undefined,
        headPicture: null,
        province: undefined,
        city: undefined,
        county: undefined,
        userId: undefined,
        status: '0',
      },
      addprop: {
        expandTrigger: 'hover',
        label: 'name',
        value: 'id',
      },
      field104: [],
      ssx: [],
      rules: {
        type: [{
          required: true,
          message: '请选择商家类型',
          trigger: 'change'
        }],
        name: [{
          required: true,
          message: '请输入商家名称',
          trigger: 'blur'
        }],
        corporation: [{
          required: true,
          message: '请输入法人',
          trigger: 'blur'
        }],
        county: [{
          required: true,
          type: 'array',
          message: '请选择地区',
          trigger: 'change'
        }],
        address: [{
          required: true,
          message: '请输入详细地址',
          trigger: 'blur'
        }],
        /*     username: [{
               required: true,
               message: '请输入管理员',
               trigger: 'blur'
             }],*/
        certNum: [{
          required: true,
          message: '请输入统一征信码',
          trigger: 'blur'
        }],
      },
      headPictureAction: '',
      headPicturefileList: [],
      field104Options: [],
      field104Props: {
        "multiple": false
      },
      typeEnums: [],
      userInfo: {},
    }
  },
  computed: {},
  watch: {},
  created() {
    this.regionList();
    this.getTypeList();
    this.getUser();
  },
  mounted() {
  },
  methods: {
    getUser() {
      getInfo().then(res => {
        console.log(res);
        this.userInfo = res.user;
        this.formData.userId = res.user.userId;
      })
    },
    regionList() {
      getRegionList().then(res => {
        console.log(res);
        this.ssx = res.msgData;
      });
    },
    handlerSuccess(res, file) {
      // debugger
      console.log("成功")
      console.log(res, file);
      this.formData.headPicture = URL.createObjectURL(file.raw);
      console.log(this.formData)
    },
    touploadHead(fileObj) {
      // debugger
      console.log("上传开始")
      console.log(fileObj);
      var picForm = new FormData();
      picForm.append('file', fileObj.file)
      auploadHead(picForm, fileObj).then(res => {
        console.log(111111)
        fileObj.onSuccess(res.url)
      });
    },
    submitForm() {
      console.log("表单", this.formData)
      this.$refs['formData'].validate(valid => {
        if (!valid) return
        // TODO 提交表单
        saveBusiness(this.formData).then(res => {
          if (res.flag) {
            this.$message.success("添加成功！");
            this.$router.push("/system/business/business");
          } else {
            this.$message.error("添加失败！")
          }
        })
      })
    },
    resetForm() {
      this.$refs['formData'].resetFields()
    },
    handleChange() {
      // TODO 发起请求获取数据
      console.log(this.field104);
      this.formData.province = this.field104[0];
      this.formData.city = this.field104[1];
      this.formData.county = this.field104[2];
    },
    getTypeList() {
      businessType().then(res => {
        console.log(res);
        this.typeEnums = res.msgData;
      })
    },
    headPictureBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 10
      if (!isRightSize) {
        this.$message.error('文件大小超过 10MB');
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
